/**
 * 
 */
package com.vmware.array2d.io;

import java.util.Scanner;

import com.vmware.array2d.util.Array2DSpiralIterable;
import com.vmware.array2d.util.Array2DSpiralIterableMetaInfo;

/**
 * The class demonstrates the usage of SpirallyIteratable2DArray.   
 * @author anshuman
 * 
 */
public class TstFeastForSpirallyIteratable2DArray {

    /**
     * build the 2Darray from the console
     * 
     * @return Array2DSpiralIterable which is a wrapper over 2DArray. The 2Darray is iterated spirally over the elements
     */
    public static Array2DSpiralIterable<String> buildArrayFromConsole() {
        Scanner console = new Scanner(System.in);
        Array2DSpiralIterableMetaInfo metaInfo = get2DArrayMetaInformationFromConsole(console);
        String[][] array2d = buildandPopulateArrayFromConsle(metaInfo,console);
        console.close();
        return new Array2DSpiralIterable<String>(array2d);
    }

    /**
     * Reads the first line from the console, get the rows# and cols# 2dArray meta information as
     * Array2DSpiralIterableMetaInfo
     * @param console 
     * 
     * @return Array2DSpiralIterableMetaInfo object which encapsulates the rows# and cols# 2dArray meta information
     */
    private static Array2DSpiralIterableMetaInfo get2DArrayMetaInformationFromConsole(Scanner console) {
        Array2DSpiralIterableMetaInfo metaInfo = null;
        System.out.println("Please enter data in the following format:\n <# rows> <# cols> \n<row 0>\n<row 1>\n...\n<row n>");
        // get rows and columns from console
        if (console.hasNextLine()) {
            Scanner lineTokenizer = new Scanner(console.nextLine());
            int rows = lineTokenizer.nextInt(); // get rows#
            int cols = lineTokenizer.nextInt(); // get cols#
            metaInfo = new Array2DSpiralIterableMetaInfo(rows, cols);
            lineTokenizer.close();
        }
        //console.close();
        return metaInfo;
    }

    /**
     * Given Array2DSpiralIterableMetaInfo, build and populate the 2DArray from the console.
     * 
     * @param metaInfo
     * @param console 
     * @return String[][]
     */
    public static String[][] buildandPopulateArrayFromConsle(Array2DSpiralIterableMetaInfo metaInfo, Scanner console) {
        String[][] array2D = new String[metaInfo.getRows()][metaInfo.getCols()];
        //Scanner console = new Scanner(System.in);
        for (int i = 0; i < array2D.length; i++) {
            Scanner lineTokenizer = new Scanner(console.nextLine());
            for (int j = 0; j < array2D[i].length; j++) {
                array2D[i][j] = lineTokenizer.next();
            }
            lineTokenizer.close();
        }
        //console.close();
        return array2D;
    }

    /**
     * Given the 2DArray, iterate through each element clockwise inward spiral printing them.
     * 
     * @param array2d
     */
    public static void printToConsole(Array2DSpiralIterable<String> array2d) {
        for (String element : array2d) {
            System.out.print(element + " ");
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        Array2DSpiralIterable<String> array2DSpiralIterable = buildArrayFromConsole();
        printToConsole(array2DSpiralIterable);
    }

}
